﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System;
using Utility;
namespace ControlBehavior
{
    public class GridViewKeyboardCommandProvider : DefaultKeyboardCommandProvider
    {
        private GridViewDataControl parentGrid;
        //private DefaultKeyboardCommandProvider defaultKeyboardProvider;
        //private GridViewKeyboardCommandProvider customKeyboardProvider;
        public delegate void ReachLastColumn_EventHandler(object sender, ReachLastColumnArgs e);
        public event ReachLastColumn_EventHandler ReachLastColumn;
        public event EventHandler TabKeyPress;

        public GridViewKeyboardCommandProvider(GridViewDataControl grid)
            : base(grid)
        {
            this.parentGrid = grid;
        }
        public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
        {
            List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
            if (key == Key.Insert)
            {
                commandsToExecute.Clear();
                commandsToExecute.Add(RadGridViewCommands.BeginEdit);
            }
            else if (key == Key.Delete)
            {
                commandsToExecute.Clear();
                commandsToExecute.Add(RadGridViewCommands.Delete);
            }
            else if (key == Key.Enter)
            {
                List<GridViewColumn> AvailableColumn = new List<GridViewColumn>();
                foreach (GridViewColumn colItem in this.parentGrid.Columns)
                {
                    if ((colItem.IsVisible == true) && (colItem.IsReadOnly == false))
                    {
                        AvailableColumn.Add(colItem);
                    }
                }
                if (AvailableColumn[AvailableColumn.Count - 1].GetHashCode() == this.parentGrid.CurrentColumn.GetHashCode())
                {
                    if (ReachLastColumn != null)
                    {
                        int index = 0;
                        foreach (object item in parentGrid.Items)
                        {   
                            if (parentGrid.CurrentCellInfo.Item.GetHashCode() == item.GetHashCode())
                            {
                                break;
                            }
                            index++;
                            
                        }
                        if (ReachLastColumn != null)
                        {
                            ReachLastColumn(this, new ReachLastColumnArgs(index));
                        }
                    }
                }
                else
                {
                    commandsToExecute.Clear();
                    commandsToExecute.Add(RadGridViewCommands.MoveNext);  
                    commandsToExecute.Add(RadGridViewCommands.BeginEdit);
               
                }
                
            }
            else if (key == Key.Tab)
            {
                
                if (TabKeyPress != null)
                {
                    TabKeyPress(this, new EventArgs());
                }
            }
            return commandsToExecute;
        }
       
    }
}
